<template>
    <el-select v-model="id" clearable :loading="loading" @visible-change="onGetData" @change="onScheduleSelect">
        <el-option label="请选择" :value="0" />
        <el-option v-for="item in data" :key="item.id" :label="item.title" :value="item.id" />
    </el-select>
</template>

<script lang="ts" setup>
import { ref,onMounted, watch } from 'vue'
import http from '../util/request';
interface Project {
    id: number
    title: string
}
const data = ref<Project[]>([])
const loading = ref<boolean>(false)
const id = ref<number>(0)
const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    id.value = props.data
},{ immediate: true })

const onGetData = () => {
    if(data.value.length > 0){
        return
    }
    
    loading.value = true
    http.post('/schedule',{}).then((response:any)=>{
        loading.value = false
        data.value = response.data.data
    })
}

const onScheduleSelect = () => {
    emit('onScheduleSelect',id.value)
}

const emit = defineEmits(['onScheduleSelect'])

onMounted(()=>{
    onGetData()
})

</script>

<style scoped>

</style>